<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Image library class
 *
 * Image information's CRUD.
 *
 */
class Img_lib
{
	/**
	 * Constructor
	 *
	 * Load all games model.
	 */
	
	public $item = array();

	public $max_size = 0;
	
	public function __construct()
	{
		$this->CI = &get_instance();
		
		$this->CI->load->helper('file');
		$this->CI->load->helper('str');
		
		$this->item = array('gif','jpg','jpeg','png','bmp');
		
		$this->max_size = '5000';
	}
	
	public function set_property($size = '309600',$item_add = array('zip'))
	{
		$this->max_size = $size;
		if(!is_array($item_add) || empty($item_add))
			return false;
		
		$this->item = array_merge($this->item,$item_add);
	}
	
	public function save_file($key, $path, $encryptname = TRUE, $file_name = '')
	{
		$config['upload_path'] = $path;
// 		$config['file_name'] = $file_name;
		$config['encrypt_name'] = $encryptname;
		$config['overwrite'] = FALSE;
		$config['allowed_types'] = join('|',$this->item);
		$config['max_size'] = $this->max_size;
		$config['max_width']  = '4096';
		$config['max_height']  = '4096';
		if(!empty($file_name)){
			$config['file_name'] = $file_name;
		}
		
		$thumb = '';
		$upload_success_flag = FALSE;
		if($this->CI->config->item('aws_enable'))
		{
			$this->CI->load->library('pf/S3_lib', $config);
			$upload_success_flag = $this->CI->s3_lib->do_upload($key);
			if($upload_success_flag){
				$data = $this->CI->s3_lib->data();
				$thumb = $config['upload_path'].$data['file_name'];
			}
		}
		elseif($this->CI->config->item('upyun_enable'))
		{
			$config['upload_path'] = $path;
			$this->CI->load->library('pf/upyun_lib',$config);
			$upload_success_flag = $this->CI->upyun_lib->do_upload($key);
			if($upload_success_flag){
				$data = $this->CI->upyun_lib->data();
				$thumb = $config['upload_path'].$data['file_name'];
			}
		}
		else
		{
			$tpath = $config['upload_path'];
			$config['upload_path'] = '../k9img/'.$tpath;
			$this->_create_folder($config['upload_path']);
			$this->CI->load->library('upload', $config);
			$upload_success_flag = $this->CI->upload->do_upload($key);
			if($upload_success_flag){
				$data = $this->CI->upload->data();
				$thumb = $config['upload_path'].$data['file_name'];
				$thumb = $tpath.$data['file_name'];
			}
		}
		
		if (!$upload_success_flag) {
			log_message('error', 'failed to save img --- '.$thumb);
		}
			
		return $thumb;
	}
	
	public function save_file_thumb($key, $path, $encryptname = TRUE, $file_name = '')
	{
		$config['upload_path'] = $path;
		// 		$config['file_name'] = $file_name;
		$config['encrypt_name'] = $encryptname;
		$config['overwrite'] = FALSE;
		$config['allowed_types'] = join('|',$this->item);
		$config['max_size'] = $this->max_size;
		$config['max_width']  = '4096';
		$config['max_height']  = '4096';
		if(!empty($file_name)){
			$config['file_name'] = $file_name;
		}
	
		$thumb = '';
		$upload_success_flag = FALSE;
		if($this->CI->config->item('aws_enable'))
		{
			$this->CI->load->library('pf/S3_lib', $config);
			$upload_success_flag = $this->CI->s3_lib->do_upload_thumb($key);
			if($upload_success_flag){
				$data = $this->CI->s3_lib->data();
				$thumb = $config['upload_path'].$data['file_name'];
			}
		}
		elseif($this->CI->config->item('upyun_enable'))
		{
			$config['upload_path'] = $path;
			$this->CI->load->library('pf/upyun_lib',$config);
			$upload_success_flag = $this->CI->upyun_lib->do_upload_thumb($key);
			if($upload_success_flag){
				$data = $this->CI->upyun_lib->data();
				$thumb = $config['upload_path'].$data['file_name'];
			}
		}
		else
		{
			$tpath = $config['upload_path'];
			$config['upload_path'] = '../k9img/'.$tpath;
			$this->_create_folder($config['upload_path']);
			$this->CI->load->library('upload', $config);
			$upload_success_flag = $this->CI->upload->do_upload_thumb($key);
			if($upload_success_flag){
				$data = $this->CI->upload->data();
				$thumb = $config['upload_path'].$data['file_name'];
				$thumb = $tpath.$data['file_name'];
			}
		}
	
		if (!$upload_success_flag) {
			log_message('error', 'failed to save img --- '.$thumb);
		}
			
		return $thumb;
	}
	
	// Create folder according path
	private function _create_folder($path) {
		return is_dir($path) or (
				$this->_create_folder(dirname($path))
				and mkdir($path, 0777));
	}
	
	// Delete specified file
	public function delete_file($fpath,$thumb=false) {
		
		if($this->CI->config->item('aws_enable')) {
			// TODO: Should be aws delete operation
			$config['upload_path'] = $fpath;
			// 		$config['file_name'] = $file_name;
			$config['encrypt_name'] = TRUE;
			$config['overwrite'] = FALSE;
			$config['allowed_types'] = join('|',$this->item);
			$config['max_size'] = $this->max_size;
			$config['max_width']  = '4096';
			$config['max_height']  = '4096';
			$this->CI->load->library('pf/S3_lib', $config);
			if(!$thumb){
				$this->CI->s3_lib->deleteObject($this->CI->s3_lib->bucket_name,$fpath);
			}else{
				$info = pathinfo($fpath);
				$s = $info['dirname'] . '/' . $info['filename'] . '_S.' . $info['extension'];
				$m = $info['dirname'] . '/' . $info['filename'] . '_M.' . $info['extension'];
				$l = $info['dirname'] . '/' . $info['filename'] . '_L.' . $info['extension'];
				$this->CI->s3_lib->deleteObject($this->CI->s3_lib->bucket_name,$s);
				$this->CI->s3_lib->deleteObject($this->CI->s3_lib->bucket_name,$m);
				$this->CI->s3_lib->deleteObject($this->CI->s3_lib->bucket_name,$l);
			}
		}
		elseif($this->CI->config->item('upyun_enable'))
		{
			$config['upload_path'] = $fpath;
			// 		$config['file_name'] = $file_name;
			$config['encrypt_name'] = TRUE;
			$config['overwrite'] = FALSE;
			$config['allowed_types'] = join('|',$this->item);
			$config['max_size'] = $this->max_size;
			$config['max_width']  = '4096';
			$config['max_height']  = '4096';
			$this->CI->load->library('pf/upyun_lib', $config);
			$info = pathinfo($fpath);
			$s = $info['dirname'] . '/' . $info['filename'] . '_S.' . $info['extension'];
			$m = $info['dirname'] . '/' . $info['filename'] . '_M.' . $info['extension'];
			$l = $info['dirname'] . '/' . $info['filename'] . '_L.' . $info['extension'];
			try
			{
				if(!$thumb)
				{
					$this->CI->upyun_lib->deleteFile('/'.$fpath);
				}
				else
				{
					$this->CI->upyun_lib->deleteFile('/'.$s);
					$this->CI->upyun_lib->deleteFile('/'.$m);
					$this->CI->upyun_lib->deleteFile('/'.$l);
				}
				
			}
			catch(Exception $e)
			{
				log_message('error',$e->getMessage());

			}
			
		} 
		else 
		{
			
			if(!$thumb){
				$fpath = '../k9img/'.$fpath;
				if(file_exists($fpath)) 
					return unlink($fpath);
			}else{
				$img_L = get_img($fpath,'L');
				$img_M = get_img($fpath,'M');
				$img_S = get_img($fpath,'S');
				
				if(!empty($fpath) && false === strpos($fpath,'http')){
					$info = pathinfo($fpath);
					$s = '../k9img/' . $info['dirname'] . '/' . $info['filename'] . '_S.' . $info['extension'];
					$m = '../k9img/' . $info['dirname'] . '/' . $info['filename'] . '_M.' . $info['extension'];
					$l = '../k9img/' . $info['dirname'] . '/' . $info['filename'] . '_L.' . $info['extension'];
					
					@unlink($s);
					@unlink($m);
					@unlink($l);
				}
			}
		}
	
		return true;
	}
}